package frontend;

import java.io.IOException;
import java.sql.Connection;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import db.RahaDAO;
import db.DbConnection;
import frontend.form.RahaForm;
import model.Raha;

@WebServlet(value={"/s"}, loadOnStartup=1)
public class RahaServlet extends HttpServlet
{
  static Logger logger = Logger.getLogger(RahaServlet.class);

  public void init()
  {
    logger.error("RahaServlet.init(): mind loodi");
    System.out.println("Mind loodi");
  }

  protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    int i = 0;
    Raha localRaha = null;
    try
    {
      List localList = getActions(req, res);
      Iterator localIterator = localList.iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        if ((str.equals("show_selected_raha")) && (i != -1))
        {
          localRaha = getRahaById(req, res);
          if (localRaha != null)
            getServletConfig().getServletContext().getRequestDispatcher("/Raha.jsp").forward(req, res);
          else
            getServletConfig().getServletContext().getRequestDispatcher("/Viga.jsp").forward(req, res);
        }
        if ((str.equals("show_selected_raha")) && (i == -1))
          getServletConfig().getServletContext().getRequestDispatcher("/Raha.jsp").forward(req, res);
        if (str.equals("show_all_raha"))
        {
          getAllRaha(req, res);
          getServletConfig().getServletContext().getRequestDispatcher("/Rahad.jsp").forward(req, res);
        }
        if (str.equals("save_raha"))
          i = saveRaha(req, res);
      }
    }
    catch (Exception localException)
    {
      logger.error("MyServlet:" + localException.getMessage());
    }
  }

  private List<String> getActions(HttpServletRequest req, HttpServletResponse res)
  {
    ArrayList localArrayList = new ArrayList();
    String str1 = "";
    String str2 = "";
    str1 = req.getParameter("id");
    str2 = req.getParameter("action");
    if ((str2 != null) && (str2.equals("save")))
      localArrayList.add("save_raha");
    if (str1 == null)
      localArrayList.add("show_all_raha");
    else if (str1.equals(""))
      localArrayList.add("show_all_raha");
    else
      localArrayList.add("show_selected_raha");
    return localArrayList;
  }

  private void getAllRaha(HttpServletRequest req, HttpServletResponse res)
  {
    Raha[] arrayOfRaha = null;
    try
    {
      Connection localConnection = DbConnection.getConnection();
      RahaDAO localRahaDAO = new RahaDAO(localConnection);
      arrayOfRaha = (Raha[])localRahaDAO.findAll();
      localConnection.close();
    }
    catch (Exception localException)
    {
      logger.error("RahaServlet.getAllRaha():" + localException.getMessage());
      localException.printStackTrace();
    }
    req.setAttribute("rahad", arrayOfRaha);
  }

  private Raha getRahaById(HttpServletRequest req, HttpServletResponse res)
  {
    Raha localRaha = null;
    try
    {
      String str = req.getParameter("id");
      int i = Integer.parseInt(str);
      Connection localConnection = DbConnection.getConnection();
      RahaDAO localRahaDAO = new RahaDAO(localConnection);
      localRaha = localRahaDAO.findById(i);
      localConnection.close();
      RahaForm localRahaForm = new RahaForm();
      localRahaForm.setId(Integer.toString(localRaha.getId()));
      localRahaForm.setNimetus(localRaha.getNimetus());
      localRahaForm.setKurss(Integer.toString(localRaha.getKurss()));
      req.setAttribute("RahaForm", localRahaForm);
      HashMap localHashMap = new HashMap();
      req.setAttribute("RahaErrors", localHashMap);
    }
    catch (Exception localException)
    {
      logger.error("RahaServlet.getRahaById():" + localException.getMessage());
      localException.printStackTrace();
    }
    return localRaha;
  }

  private int saveRaha(HttpServletRequest req, HttpServletResponse res)
  {
    int i = 1;
    Raha localRaha = null;
    try
    {
      String str1 = req.getParameter("id");
      String str2 = req.getParameter("raha_nimetus");
      String str3 = req.getParameter("kurss");
      RahaValidator localRahaValidator = new RahaValidator();
      RahaForm localRahaForm = new RahaForm();
      localRahaForm.setId(str1);
      localRahaForm.setNimetus(str2);
      localRahaForm.setKurss(str3);
      HashMap localHashMap = localRahaValidator.Validate(localRahaForm);
      if (localHashMap.size() > 0)
      {
        req.setAttribute("RahaForm", localRahaForm);
        req.setAttribute("RahaErrors", localHashMap);
        i = -1;
      }
      else
      {
        Connection localConnection = DbConnection.getConnection();
        RahaDAO localRahaDAO = new RahaDAO(localConnection);
        localRaha = new Raha();
        localRaha.setId(Integer.parseInt(str1));
        localRaha.setNimetus(str2);
        localRaha.setKurss(Integer.parseInt(str3));
        localRahaDAO.update(localRaha);
        localConnection.close();
      }
    }
    catch (Exception localException)
    {
      logger.error("RahaServlet.saveRaha():" + localException.getMessage());
      localException.printStackTrace();
      i = -1;
    }
    return i;
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException
  {
    doGet(req, res);
  }
}